package com.ideaction.game.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;


import lombok.extern.slf4j.Slf4j;

/**
 * 本地化
 *
 */
@Slf4j
public class Localization {

	public static String DEFAULT_LANG = "EN";
	
	public static String CN = "CN";

	private static Localization localization;

	private static String[] replaceString = { "{0}", "{1}", "{2}", "{3}", "{4}", "{5}", "{6}", "{7}" };

	public static Localization getInstance() {
		if (localization == null)
			localization = new Localization();
		return localization;
	}

	/**
	 * 库
	 */
	HashMap<String, HashMap<String, String>> localizationMap = new HashMap<String, HashMap<String, String>>();

	/**
	 * 加载本地化翻译文本
	 *
	 * @param file
	 */
	public void init(String fileName) {
		try {
			log.info(" Localization init. fileName=" + fileName);
			File file = new File(fileName);
			BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
			// 第一行
			String str = in.readLine();
			// KEY,CN,EN......
			String[] titles = str.split(",");
			while ((str = in.readLine()) != null) {
				if (str.equals(""))
					continue;
				String[] contants = str.split(",\"");
				HashMap<String, String> map = new HashMap<String, String>();
				for (int i = 1; i < contants.length; i++) {
					String s = contants[i];
					if (s.endsWith(",")) {
						s = s.substring(0, s.length() - 1);
					}
					if (s.endsWith("\"")) {
						s = s.substring(0, s.length() - 1);
					}
					map.put(titles[i], s);
				}
				localizationMap.put(contants[0], map);
			}
			in.close();
		} catch (Exception ex) {
			log.warn(" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> location txt load err.", ex);
		}
	}

	/**
	 * 获得文本
	 *
	 * @param key
	 *            键
	 * @param lang
	 *            语言
	 * @return
	 */
//	public String get(String key) {
//		if (key == null) {
//			return "";
//		}
//		HashMap<String, String> map = localizationMap.get(key);
//		if (map == null)
//			return key;
//		String value = map.get("CN");
//		if (value == null)
//			value = map.get(DEFAULT_LANG);
//		if (value == null)
//			return key;
//		return value;
//	}
	
	/**
	 * 获得文本
	 *
	 * @param key
	 *            键
	 * @param lang
	 *            语言
	 * @return
	 */
	public String get(String key, String lang) {
		if(StringUtils.isEmpty(lang)){
			lang = CN;
		}
		if (key == null) {
			return "";
		}
		HashMap<String, String> map = localizationMap.get(key);
		if (map == null)
			return key;
		String value = map.get(lang);
		if (value == null)
			value = map.get(DEFAULT_LANG);
		if (value == null)
			return key;
		return value;
	}

	/**
	 * 获得文本
	 *
	 * @param key
	 *            键
	 * @param lang
	 *            语言
	 * @return
	 */
	public String get(String key, String lang, String... strings) {
		String value = this.get(key, lang);
		for (int i = 0; i < strings.length; i++) {
			value = value.replace(replaceString[i], strings[i]);
		}
		return value;
	}

	/**
	 * 获得文本
	 *
	 * @param key
	 *            键
	 * @param lang
	 *            语言
	 * @return
	 */
	public String get(String key, String lang, Collection<String> strings) {
		String value = this.get(key, lang);
		int i = 0;
		for (String str : strings) {
			value = value.replace(replaceString[i], str);
			i++;
		}
		return value;
	}

	public Set<String> getAllLang() {
		return localizationMap.keySet();
	}
}
